package com.halloon.android.ui.activity;

import java.util.ArrayList;

import com.halloon.android.R;
import com.halloon.android.ui.fragment.CommentFragment;
import com.halloon.android.ui.fragment.CommentFragment.CommentFragmentCallback;
import com.halloon.android.ui.fragment.TabMainPageFragment;
import com.halloon.android.ui.fragment.TabMainPageFragment.MainPageFragmentCallback;
import com.halloon.android.ui.fragment.TabProfileFragment;
import com.halloon.android.ui.fragment.TabProfileFragment.ProfileFragmentCallback;
import com.halloon.android.ui.fragment.TweetDetailFragment;
import com.halloon.android.ui.fragment.TweetDetailFragment.TweetDetailFragmentCallback;
import com.halloon.android.util.PopupWindowManager;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;

public class BaseMultiFragmentActivity extends BaseSinglePaneActivity implements MainPageFragmentCallback,
                                                                                 TweetDetailFragmentCallback,
                                                                                 ProfileFragmentCallback,
                                                                                 CommentFragmentCallback{
	
	protected ArrayList<Fragment> mFragmentContainer;
	protected PopupWindowManager mPopupWindowManager;
	
	protected FragmentManager mFragmentManager;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		mFragmentContainer = new ArrayList<Fragment>();
		mPopupWindowManager = new PopupWindowManager(this);
		mFragmentManager = getSupportFragmentManager();
		
		super.onCreate(savedInstanceState);
		
		
		homeGoToDashboard = false;
	}

	@Override
	protected Fragment onCreatePane() {
		return mFragment;
	}

	@Override
	protected void init() {}
	
	@Override
	protected void onPostCreate(Bundle savedInstanceState){
		super.onPostCreate(savedInstanceState);
	}
	
	protected void backStackAction(){
		mFragmentManager.popBackStack();
		mFragmentContainer.remove(mFragmentContainer.size() - 1);
	}

	@Override
	public void setupTweetListFragment(Bundle bundle) {
		mFragmentContainer.add(new TabMainPageFragment());
		getFragmentDec(1).setArguments(bundle);
		((TabMainPageFragment) getFragmentDec(1)).setTweetState(TabMainPageFragment.OTHER_TWEET);
		FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
		fragmentTransaction.setCustomAnimations(R.anim.fragment_slide_right_enter, 
				                                R.anim.fragment_slide_left_exit, 
				                                R.anim.fragment_slide_left_enter, 
				                                R.anim.fragment_slide_right_exit);
		fragmentTransaction.add(R.id.root_container, getFragmentDec(1));
		fragmentTransaction.hide(getFragmentDec(2));
		fragmentTransaction.addToBackStack(null);
		fragmentTransaction.commit();
	}

	@Override
	public void setupProfileFragment(Bundle bundle) {
		mFragmentContainer.add(new TabProfileFragment());
		getFragmentDec(1).setArguments(bundle);
		((TabProfileFragment) getFragmentDec(1)).setType(TabProfileFragment.OTHER);
		FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
		fragmentTransaction.setCustomAnimations(R.anim.fragment_slide_right_enter,
				                                R.anim.fragment_slide_left_exit,
				                                R.anim.fragment_slide_left_enter,
				                                R.anim.fragment_slide_right_exit);
		fragmentTransaction.add(R.id.root_container, getFragmentDec(1));
		fragmentTransaction.hide(getFragmentDec(2));
		fragmentTransaction.addToBackStack(null);
		fragmentTransaction.commit();
	}

	@Override
	public void setupDetailFragment(Bundle bundle) {
		mFragmentContainer.add(new TweetDetailFragment());
		getFragmentDec(1).setArguments(bundle);
		FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
		fragmentTransaction.setCustomAnimations(R.anim.fragment_slide_right_enter,
				                                R.anim.fragment_slide_left_exit,
				                                R.anim.fragment_slide_left_enter,
				                                R.anim.fragment_slide_right_exit);
		fragmentTransaction.add(R.id.root_container, getFragmentDec(1));
		fragmentTransaction.hide(getFragmentDec(2));
		fragmentTransaction.addToBackStack(null);
		fragmentTransaction.commit();
	}
	
	@Override
	public void setupCommentFragment(Bundle bundle){
		mFragmentContainer.add(new CommentFragment());
		getFragmentDec(1).setArguments(bundle);
		FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
		fragmentTransaction.setCustomAnimations(R.anim.fragment_slide_right_enter,
				                                R.anim.fragment_slide_left_exit,
				                                R.anim.fragment_slide_left_enter,
				                                R.anim.fragment_slide_right_exit);
		fragmentTransaction.add(R.id.root_container, getFragmentDec(1));
		fragmentTransaction.hide(getFragmentDec(2));
		fragmentTransaction.addToBackStack(null);
		fragmentTransaction.commit();
	}

	@Override
	public void setupPublishFragment() {
	}

	@Override
	public void setupAroundTweetFragment() {
	}
	
	@Override
	public void setupPictureDialog(String addr, String preSize) {
		mPopupWindowManager.setupPicturePopup(addr, preSize);
	}
	
	@Override
	public void mListChange(int status) {
		((TweetDetailFragment) getFragmentDec(1)).changeMList(status);
	}
	
	@Override
	public void destroyProfileFragment() {
		backStackAction();
	}
	
	@Override
	public void destroyMainPageFragment() {
		backStackAction();
	}
	
	@Override
	public void destroyDetailFragment() {
		backStackAction();
	}
	
	@Override
	public void destroyCommentFragment() {
		backStackAction();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event){
		if(event.getKeyCode() == KeyEvent.KEYCODE_BACK && mFragmentContainer.size() > 1){
			backStackAction();
			return true;
		}
		
		return false;
	}
	
	protected Fragment getFragmentDec(int decCount){
		return mFragmentContainer.get(mFragmentContainer.size() - decCount);
	}

}
